<!--登录-->

<template>
  <div>
    <el-form :model="form" :rules="rules"
             status-icon
             ref="form"
             label-position="left"
             label-width="0px"
             class="demo-ruleForm login-page"
             @keyup.enter="login()"
    >
      <h3 class="title" style="margin-bottom:20px ">系统登录</h3>
      <el-form-item prop="username">
        <el-input type="text"
                  v-model="form.username"
                  auto-complete="off"
                  placeholder="用户名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password"
                  v-model="form.password"
                  auto-complete="off"
                  placeholder="密码"
        ></el-input>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:100%;" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>


<script>
import request from "@/utils/request";
export default {
  data() {
 return {
      loadering: true,
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [{required: true, message: '请输入用户名', trigger: 'blur'}],   //登录时验证用户名密码是否为空
        password: [{required: true, message: '请输入密码', trigger: 'blur'}]
      },
    }
  },
  methods: {
    login() {
      this.$refs['form'].validate(async valid => {
        if (!valid) return

        const res = await request.post("/user/login", this.form)

        if (res.code === "200") {
          //弹出登陆成功框
          this.$message.success("登录成功!")
          //缓存登录用户,将对象转换为 JSON
          localStorage.setItem("user", JSON.stringify(res.data))
          //登录成功后跳转到主页
          this.$router.push("/personalCenter");
        } else {
          console.log(res);
          this.$message.error(res.msg)
        }
      })
    }
  }
};
</script>

<style scoped>
.login-page {
  -webkit-border-radius: 5px;
  border-radius: 5px;
  margin: 220px auto;
  width: 350px;
  padding: 35px 35px 15px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  border-radius: 15px;
}
</style>
